import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_chubao/tools/app_log.dart';
import 'package:flutter_chubao/widgets/app_widget_appbar.dart';
import 'package:get/get.dart';

import 'app_web_logic.dart';
import 'app_web_state.dart';

/// @description:
/// @author
/// @date: 2023-12-15 10:30:14
class AppWebPage extends StatelessWidget {
  final AppWebLogic logic = Get.put(AppWebLogic());
  final AppWebState state = Get.find<AppWebLogic>().state;

  AppWebPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppWidgetAppbar(
        titleStr: state.title,
      ),
      body: Stack(
        children: [
          InAppWebView(
              initialData: state.url.startsWith('http')
                  ? null
                  : InAppWebViewInitialData(data: state.url),
              initialUrlRequest: state.url.startsWith('http')
                  ? URLRequest(url: Uri.parse(state.url))
                  : null,
              // initialData: InAppWebViewInitialData(data: state.htmlStr),
              initialOptions: InAppWebViewGroupOptions(
                  crossPlatform: InAppWebViewOptions(
                    useShouldOverrideUrlLoading: true,
                    javaScriptEnabled: true,
                    useOnDownloadStart: true,
                    useOnLoadResource: true,
                  ),
                  ios: IOSInAppWebViewOptions(
                    allowsInlineMediaPlayback: true,
                  ),
                  android: AndroidInAppWebViewOptions(
                      // useShouldOverrideUrlLoading
                      // useShouldInterceptRequest: ,
                      // loadWithOverviewMode: false,

                      databaseEnabled: true,
                      domStorageEnabled: true)),
              onWebViewCreated: (InAppWebViewController controller) {
                state.webViewController = controller;
              },
              onConsoleMessage: (controller, consoleMessage) {
                DLog("consoleMessage ${consoleMessage.message}");
                // DLog(consoleMessage);
//               // it will print: {message: {"bar":"bar_value","baz":"baz_value"}, messageLevel: 1}
              },
              onProgressChanged: (controller, progress) {
                state.progress = progress / 100;
                logic.update(['progress']);
              },
              // onLoadStart: (controller, uri) {
              //   DLog('onLoadStart  $uri');
              // },
              // onLoadError: (c, uri, coint, info) {
              //   DLog("onLoadError  $info");
              // },
              onLoadStop: (controller, url) {
                // String requestUrl = url.toString();

                // _webViewController.addWebMessageListener(webMessageListener)
              },
              onPrint: (controller, url) {
                DLog('url$url');
              },
              onLoadResource: (contoller, resource) {
                DLog('onLoadResource $resource');
              },
              // initialUserScripts: UnmodifiableListView<UserScript>([]),
              // initialOptions: InAppWebViewGroupOptions(
              //     crossPlatform: InAppWebViewOptions(
              //       useShouldOverrideUrlLoading: true,
              //       javaScriptEnabled: true,
              //       useOnDownloadStart: true,
              //       useOnLoadResource: true,
              //     ),
              //     android: AndroidInAppWebViewOptions(
              //         databaseEnabled: true, domStorageEnabled: true),
              //     ios: IOSInAppWebViewOptions(
              //       allowsInlineMediaPlayback: true,
              //     )),
              androidOnPermissionRequest: (InAppWebViewController controller,
                  String origin, List<String> resources) async {
                return PermissionRequestResponse(
                    resources: resources,
                    action: PermissionRequestResponseAction.GRANT);
              }),
          GetBuilder<AppWebLogic>(
            id: 'progress',
            builder: (controller) {
              return state.progress < 1.0
                  ? LinearProgressIndicator(value: state.progress)
                  : Container();
            },
          ),
        ],
      ),
    );
  }
}
